package com.example.administrator.ygddriver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.ygddriver.appliedorder.AppliedOrderActivity;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.LogInformationBean;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.listen.ListenFragment;
import com.example.administrator.ygddriver.listen.grab.GrabActivity;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.message.MessageActivity;
import com.example.administrator.ygddriver.netty.TcpClient;
import com.example.administrator.ygddriver.order.OrderActivity;
import com.example.administrator.ygddriver.set.SetActivity;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.UrlValuse;
import com.example.administrator.ygddriver.weixinnum.WeiXinNumActivity;

import java.io.File;
import java.util.HashMap;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private TextView mListenTv;
    private FrameLayout mFl;
    private FragmentTransaction transaction;
    private MapFragment mMapFragment;
    private ListenFragment mListenFragment;
    private Boolean tag = false;
    private ImageView mOrderIv, mMyIv;
    private DrawerLayout drawerLayout;
    private LinearLayout mAppliedOrderLl, mSetLl, mMyMoneyLl, mMessageLl;
    private String name, city, account, id, token;
    private ImageView mHeadIv;
    private TextView mNameTv, mGetmoneyTv, mfinisTv, mGetBillTv;

    @Override
    public int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(UrlValuse.GET_MAIN_SX);
        registerReceiver(BdBroadcastReceiver, myIntentFilter);

        mAppliedOrderLl = findViewById(R.id.activity_main_applied_order_ll);
        mListenTv = findViewById(R.id.activity_main_listen_tv);
        mFl = findViewById(R.id.activity_main_fl);
        mOrderIv = findViewById(R.id.activity_main_order_iv);
        mMyIv = findViewById(R.id.activity_main_my_iv);
        drawerLayout = findViewById(R.id.activity_main_dr);
        mSetLl = findViewById(R.id.activity_main_more_set_ll);
        mNameTv = findViewById(R.id.activity_main_name_iv);
        mHeadIv = findViewById(R.id.activity_main_head_iv);
        mMyMoneyLl = findViewById(R.id.activity_main_income_ll);
        mMessageLl = findViewById(R.id.activity_main_message_ll);
        mGetmoneyTv = findViewById(R.id.activity_main_get_money_tv);
        mfinisTv = findViewById(R.id.activity_main_finis_num_tv);
        mGetBillTv = findViewById(R.id.activity_main_get_bill_tv);
        mMessageLl.setOnClickListener(this);
        mMyMoneyLl.setOnClickListener(this);
        mSetLl.setOnClickListener(this);
        mMyIv.setOnClickListener(this);
        mAppliedOrderLl.setOnClickListener(this);
        mOrderIv.setOnClickListener(this);
        mListenTv.setOnClickListener(this);
        mMapFragment = new MapFragment();

    }

    @Override
    public void initDate() {
        TcpClient instance_t = TcpClient.getInstance();
        instance_t.sendMsgByByete();
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.activity_main_fl, mMapFragment);
        transaction.commit();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_main_listen_tv:
                LiteOrmTools.getInformation(LogInformationBean.class, this, new LiteOrmTools.On<LogInformationBean>() {
                    @Override
                    public void hasData(LogInformationBean data) {
                        city = data.getC_city();
                    }
                }, new LiteOrmTools.OFF() {
                    @Override
                    public void noData() {

                    }
                });

                transaction = getSupportFragmentManager().beginTransaction();
                if (tag) {

                    // 停止听单(c_account 司机端账号唯一标识 c_city 服务城市 c_operation 操作类型)
                    String msg2 = "{c_account:" + id + ",c_city:" + city + ",c_operation:2}&";
                    TcpClient instance_t = TcpClient.getInstance();
                    instance_t.sendMsgByByete(msg2);

                    tag = false;
                    mListenTv.setText("开始听单");
                    Resources resources = getBaseContext().getResources();
                    Drawable drawable = resources.getDrawable(R.mipmap.start_listen);
                    mListenTv.setBackground(drawable);
                    mListenTv.setTextColor(this.getResources().getColor(R.color.white));
                    transaction.hide(mListenFragment);
                    transaction.show(mMapFragment);
                } else {
                    // 开始听单(c_account 司机端账号唯一标识 c_city 服务城市 c_operation 操作类型)
                    String msg1 = "{c_account:" + id + ",c_city:" + city + ",c_operation:1}&";
                    TcpClient instance_t = TcpClient.getInstance();
                    instance_t.sendMsgByByete(msg1);
                    tag = true;
                    mListenTv.setText("停止听单");
                    Resources resources = getBaseContext().getResources();
                    Drawable drawable = resources.getDrawable(R.mipmap.stop_listen);
                    mListenTv.setBackground(drawable);
                    mListenTv.setTextColor(this.getResources().getColor(R.color.listen));
                    if (mListenFragment == null) {
                        mListenFragment = new ListenFragment();
                        transaction.add(R.id.activity_main_fl, mListenFragment);
                        transaction.hide(mMapFragment);
                    } else {
                        transaction.hide(mMapFragment);
                        transaction.show(mListenFragment);
                    }

                }
                transaction.commit();
                break;
            case R.id.activity_main_order_iv:
                Intent intent = new Intent(this, OrderActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_main_my_iv:
                //如果抽屉关闭就打开，如果抽屉打开就关闭
                if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
                    drawerLayout.closeDrawer(Gravity.LEFT);
                } else {//如果已经是关闭状态
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
                break;
            case R.id.activity_main_applied_order_ll:
                Intent intent1 = new Intent(this, AppliedOrderActivity.class);
                startActivity(intent1);
                break;
            case R.id.activity_main_more_set_ll:
                Intent intent2 = new Intent(this, SetActivity.class);
                startActivity(intent2);

                break;
            case R.id.activity_main_income_ll:
                Intent intent3 = new Intent(this, WeiXinNumActivity.class);
                startActivity(intent3);
                break;
            case R.id.activity_main_message_ll:
                Intent intent4 = new Intent(this, MessageActivity.class);
                startActivity(intent4);
                break;


        }

    }

    //今日收入和完成订单
    private void initRevenueAndOrderHtml(String c_driver, String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_driver", c_driver);
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", UrlValuse.TYPE);
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_DRIVER + UrlValuse.REVENUE_AND_ORDER, hashMap, RevenueAndOrderBean.class, new OkGoRequests.OnTrue<RevenueAndOrderBean>() {
            @Override
            public void hasData(RevenueAndOrderBean date) {
                if (date.isSuccess()) {
                    mGetBillTv.setText(date.getResult().getC_size());
                    mGetmoneyTv.setText(date.getResult().getC_revenue());
                    mfinisTv.setText(date.getResult().getC_completeOrder());
                }
            }
        }, new OkGoRequests.OnError<RevenueAndOrderBean>() {
            @Override
            public void noData() {

            }
        });
    }

    private BroadcastReceiver BdBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (TextUtils.isEmpty(id) || TextUtils.isEmpty(token)) {

            } else {
                initRevenueAndOrderHtml(id, token);
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        LiteOrmTools.getInformation(UserBean.class, this, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                id = data.getC_id();
                token = data.getC_token();
                account = data.getC_account();
                RequestOptions requestOptions = new RequestOptions()
                        .placeholder(R.mipmap.big_head)
                        .error(R.mipmap.big_head)
                        .fallback(R.mipmap.big_head);
                String s = String.valueOf(Environment.getExternalStorageDirectory());
                Glide.with(MainActivity.this)
                        .load(s + "/YGDDriver/Photo/" + data.getC_account() + "HEAD_PHOTO.jpg")
                        .apply(requestOptions)
                        .into(mHeadIv);
                data.getC_account();
                initRevenueAndOrderHtml(id, token);
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        LiteOrmTools.getInformation(LogInformationBean.class, this, new LiteOrmTools.On<LogInformationBean>() {
            @Override
            public void hasData(LogInformationBean data) {
                name = data.getC_name();
                mNameTv.setText(name);
                city = data.getC_city();
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
    }
}
